/**
 * Copyright (c) 2017 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
 */

package com.hyyt.shopjsp.wxmg.fans.controller;

import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.json.FastJsonUtils;
import com.hyyt.shopjsp.wxmg.fans.pojo.WxFansGroup;
import com.hyyt.shopjsp.wxmg.fans.pojo.WxFansUserInfo;
import com.hyyt.shopjsp.wxmg.fans.service.IWxFansGroupService;
import com.hyyt.shopjsp.wxmg.fans.service.IWxFansUserInfoService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Liu Ling on 2017/11/10 0010.
 *
 * 粉丝用户信息
 */
@Controller
@RequestMapping("/back/wx/fansUserInfo")
public class WxFansUserInfoController extends BaseController {

    private Logger logger = Logger.getLogger(WxFansUserInfoController.class);
    private String basePath = "/back/wxmg/fansUserInfo/";
    @Resource
    private IWxFansUserInfoService wxFansUserInfoService;
    @Resource
    private IWxFansGroupService wxFansGroupService;

    /**
     *进入粉丝用户信息页面
     * @param param 参数
     * @return 返回到指定页面
     */
    @RequestMapping("gotoFansUserInfoPage")
    public String gotoFansUserInfoPage (Map<String,Object> param){
        List<WxFansGroup> wxFansGroupList = wxFansGroupService.queryAllWxFansGroup();
        param.put("wxFansGroupList",wxFansGroupList);
        return basePath + "fansUserInfoList";
    }

    /**
     *查询所有粉丝用户信息列表
     *
     * @param loginName 会员名称
     * @param plateFormRemark 名称备注
     * @param subscribeBeginTime 关注开始时间
     * @param subscribeEndTime 关注结束时间
     * @param subscribe 是否关注
     * @param fansGroupId 组别
     * @param page 分页
     * @param response 返回结果集
     */
    @RequestMapping(value = "listFansUserInfo",method = RequestMethod.POST)
    @ResponseBody
    public void listFansUserInfo(String loginName, String plateFormRemark, String subscribeBeginTime, String subscribeEndTime, String subscribe, String fansGroupId, Page page, HttpServletResponse response){
        Map<String,Object> param = new HashMap<>();
        if (Utils.objectIsNotEmpty(loginName)) {
            param.put("loginName", loginName.trim());
        }
        if (Utils.objectIsNotEmpty(plateFormRemark)) {
            param.put("plateFormRemark", plateFormRemark.trim());
        }
        if(Utils.objectIsNotEmpty(subscribeBeginTime)){
            param.put("subscribeBeginTime",subscribeBeginTime);
        }
        if(Utils.objectIsNotEmpty(subscribeEndTime)){
            param.put("subscribeEndTime",subscribeEndTime);
        }
        if (Utils.objectIsNotEmpty(subscribe)) {
            param.put("subscribe", subscribe);
        }
        if (Utils.objectIsNotEmpty(fansGroupId)) {
            param.put("fansGroupId", fansGroupId);
        }
        int totalRecordCount = wxFansUserInfoService.countWxFansUserInfo(param);
        param.put("sortColumns","userId desc");
        page.setPageInfo(page.getPageSize(),totalRecordCount,page.getCurrentPage());
        List<Map <String , Object>> list = wxFansUserInfoService.queryFansUserInfoAndCustomer(param,page.getPageRecordBeginIndex(),page.getPageSize());
        param.clear();
        param.put("total",totalRecordCount);
        param.put("rows",list);
        ResultMsg.create(param,response);
    }

    /**
     * 根据id获得粉丝用户信息对象
     * @param userId 粉丝用户id
     * @return
     */
    @RequestMapping(value = "getFansUserInfoObject",method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg getFansUserInfoObject(Integer userId){
        if (Utils.objectIsNotEmpty(userId)){
            WxFansUserInfo wxFansUserInfo = wxFansUserInfoService.loadWxFansUserInfo(userId);
            return ResultMsg.create(true,null, FastJsonUtils.toJSONString(wxFansUserInfo));
        }else{
            return ResultMsg.create(false);
        }
    }

    /**
     * 根据id删除粉丝用户信息
     * @param userId 粉丝用户信息id
     * @return 返回布尔值
     */
    @RequestMapping(value = "deleteFansUserInfo",method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg deleteFansUserInfo(Integer userId){
        if (Utils.objectIsNotEmpty(userId)){
            WxFansUserInfo wxFansUserInfo=wxFansUserInfoService.loadWxFansUserInfo(userId);
            WxFansGroup wxFansGroup=wxFansGroupService.loadWxFansGroup(wxFansUserInfo.getFansGroupId());
            wxFansUserInfoService.deleteWxFansUserInfo(userId);
            Map<String,Object> param = new HashMap<>();
            param.put("fansGroupId",wxFansUserInfo.getFansGroupId());
            wxFansGroup.setCount(wxFansUserInfoService.countWxFansUserInfo(param));//删除粉丝用户时粉丝分组表中的人数值发生变化
            wxFansGroupService.updateWxFansGroup(wxFansGroup);
            return ResultMsg.create(true);
        }else{
            return ResultMsg.create(false);
        }
    }

    /**
     *查询未关联粉丝分组列表
     *
     * @param fansGroupId 粉丝分组ID
     * @param name 组名称
     * @param page 分页
     * @param response 返回结果集
     */
    @RequestMapping(value = "UnSelectedFansGroupList",method = RequestMethod.POST)
    @ResponseBody
    public void UnSelectedFansGroupList(String fansGroupId, String name, Page page, HttpServletResponse response){
        Map<String,Object> param = new HashMap<>();
        if (Utils.objectIsNotEmpty(fansGroupId)) {
            param.put("fansGroupId", fansGroupId);
        }
        if (Utils.objectIsNotEmpty(name)) {
            param.put("name", name.trim());
        }
        int totalRecordCount = wxFansGroupService.countUnSelectedFansGroupList(param);
        param.put("sortColumns","fansGroupId desc");
        page.setPageInfo(page.getPageSize(),totalRecordCount,page.getCurrentPage());
        List<Map<String,Object>> list = wxFansGroupService.queryUnSelectedFansGroupList(param,page.getPageRecordBeginIndex(),page.getPageSize());
        param.clear();
        param.put("total",totalRecordCount);
        param.put("rows",list);
        ResultMsg.create(param,response);
    }

    /**
     * 切换粉丝分组
     * @param userId 粉丝用户信息id
     * @param fansGroupId 粉丝分组id
     * @return 返回布尔值
     */
    @RequestMapping(value = "changeFansGroup",method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg changeFansGroup(Integer userId,Integer fansGroupId){
        if (Utils.objectIsNotEmpty(userId)){
            WxFansUserInfo wxFansUserInfo=wxFansUserInfoService.loadWxFansUserInfo(userId);
            wxFansUserInfo.setFansGroupId(fansGroupId);
            wxFansUserInfoService.updateWxFansUserInfo(wxFansUserInfo);
            WxFansGroup wxFansGroup=wxFansGroupService.loadWxFansGroup(fansGroupId);
            Map<String,Object> param = new HashMap<>();
            param.put("fansGroupId",fansGroupId);
            wxFansGroup.setCount(wxFansUserInfoService.countWxFansUserInfo(param));//给粉丝进行分组时人数发生变化
            wxFansGroupService.updateWxFansGroup(wxFansGroup);
            return ResultMsg.create(true);
        }else {
            return ResultMsg.create(false);
        }
    }

    /**
     * 修改名称备注
     * @param userId 粉丝用户信息id
     * @param plateFormRemark 名称备注
     * @return 返回布尔值
     */
    @RequestMapping(value = "changePlateFormRemark",method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg changePlateFormRemark(Integer userId,String plateFormRemark){
        if (Utils.objectIsNotEmpty(userId)){
            WxFansUserInfo wxFansUserInfo=wxFansUserInfoService.loadWxFansUserInfo(userId);
            wxFansUserInfo.setPlateFormRemark(plateFormRemark);
            wxFansUserInfoService.updateWxFansUserInfo(wxFansUserInfo);
            return ResultMsg.create(true);
        }else {
            return ResultMsg.create(false);
        }
    }

}
